// $Id$
////////////////////////////////////////////////////////////////
// Porpose : learn usage of opendir,readdir
// Date    : 2011.07.28
// By      : mygoddes@yeah.net
////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <string.h>
#include <dirent.h>

#define WRONG_ARGS -1

const char *kHelpMsg = "example : ./a.out .\n";

void Usage()
{
	printf(kHelpMsg);
	return;
}

void OutPutDirByreaddir(const char *dirpath)
{
	DIR *dp = NULL;
	struct dirent *dirp = NULL;

	dp = opendir(dirpath);

	if( !dp )
	{
		printf("%s not exist\n", dirpath);
		return;
	}

	printf("Current dir is %s\n\t", dirpath);
	while((dirp = readdir(dp)))
	{
		printf("%s\n\t\t", dirp->d_name);
	}

	closedir(dp);

	return;
}

void OutPutDirByreaddirr(const char *dirpath)
{
	DIR *dp = NULL;
	struct dirent entry, *p_entry = NULL;

	dp = opendir(dirpath);
	if (!dp)
	{
		printf("%s not exist\n", dirpath);
		return;
	}

	printf("Current dir is %s\n\t", dirpath);
	readdir_r(dp, &entry, &p_entry);
	while (p_entry)
	{
		printf("%s\n\t\t", entry.d_name);
		readdir_r(dp, &entry, &p_entry);
	}
	closedir(dp);

	return;
}

int main(int argc, const char **argv)
{
	if(argc < 2)
	{
		Usage();
		return WRONG_ARGS;
	}

	OutPutDirByreaddir(argv[1]);
	OutPutDirByreaddirr(argv[1]);

	return 1;
}
